package com.mcxx.modules.minimumliving.service.impl;

import cn.hutool.core.date.DateField;
import com.mcxx.common.exception.BizException;
import com.mcxx.modules.minimumliving.service.AreaMoveService;
import com.mcxx.modules.base.utils.QueryParamUtil;
import com.mcxx.modules.filemanage.easypoi.util.ExportExcelService;
import com.mcxx.modules.filemanage.file.service.FileService;
import com.mcxx.modules.miniSalvation.family.dto.SalvationFamilyExcelDTO;
import com.mcxx.modules.miniSalvation.family.entity.FamilyMemberEntity;
import com.mcxx.modules.miniSalvation.family.service.read.FamilyMemberReadService;
import com.mcxx.modules.miniSalvation.family.service.write.FamilyMainHistoryWriteService;
import com.mcxx.modules.miniSalvation.family.service.write.FamilyMemberHistoryWriteService;
import com.mcxx.modules.miniSalvation.fundpay.service.read.FundPayReadService;
import com.mcxx.modules.miniSalvation.order.dao.write.OrderCertWriteDao;
import com.mcxx.modules.miniSalvation.order.dto.*;
import com.mcxx.modules.miniSalvation.order.entity.*;
import com.mcxx.modules.miniSalvation.order.param.MoveAreaParam;
import com.mcxx.modules.miniSalvation.order.service.read.*;
import com.mcxx.modules.miniSalvation.order.service.write.*;
import com.mcxx.modules.miniSalvation.salary.entity.FamilySalaryEntity;
import com.mcxx.modules.miniSalvation.salary.entity.MemberSalaryEntity;
import com.mcxx.modules.miniSalvation.salary.service.read.FamilyMemberSalaryReadService;
import com.mcxx.modules.miniSalvation.salary.service.read.FamilySalaryReadService;
import com.mcxx.modules.miniSalvation.salary.service.write.FamilyMemberSalaryWriteService;
import com.mcxx.modules.miniSalvation.salary.service.write.FamilySalaryWriteService;
import com.mcxx.modules.miniSalvation.salary.service.write.SalaryChangeWriteService;
import com.mcxx.modules.system.entity.TaskExportEntity;
import com.mcxx.modules.system.service.write.TaskExportWriteService;
import com.mcxx.result.BaseResult;
import com.mcxx.util.CommonUtil;
import com.mcxx.util.Constant;
import com.mcxx.util.DateUtil;
import com.mcxx.util.FailureCode;
import com.yinhai.ta404.component.audit.vo.ExcelInfo;
import com.yinhai.ta404.core.security.vo.UserAccountVo;
import com.yinhai.ta404.core.transaction.annotation.TaTransactional;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Slf4j
@Service
@TaTransactional
public class AreaMoveServiceImpl implements AreaMoveService {
    @Autowired
    private AreaMoveRecordReadService areaMoveRecordReadService;
    @Autowired
    private AreaMoveRecordWriteService areaMoveRecordWriteService;
    @Autowired
    private AreaMoveReadService areaMoveReadService;
    @Autowired
    private AreaMoveWriteService areaMoveWriteService;
    @Autowired
    private OrderWriteService orderWriteService;
    @Autowired
    private OrderReadService orderReadService;
    @Autowired
    private OrderDetailWriteService orderDetailWriteService;
    @Autowired
    private FamilySalaryWriteService familySalaryWriteService;
    @Autowired
    private FamilyMemberSalaryWriteService familyMemberSalaryWriteService;
    @Autowired
    private FamilyMainHistoryWriteService familyMainHistoryWriteService;
    @Autowired
    private FamilyMemberHistoryWriteService familyMemberHistoryWriteService;
    @Autowired
    private FundPayReadService fundPayReadService;
    @Autowired
    private OrderCertWriteService orderCertWriteService;
    @Autowired
    private ExportExcelService exportExcelService;
    @Autowired
    private FileService fileService;
    @Autowired
    private TaskExportWriteService taskExportWriteService;
    @Autowired
    private OrderCertWriteDao orderCertWriteDao;
    @Autowired
    private FamilySalaryReadService familySalaryReadService;
    @Autowired
    private FamilyMemberSalaryReadService familyMemberSalaryReadService;
    @Autowired
    private SalaryChangeWriteService salaryChangeWriteService;
    @Autowired
    private FamilyMemberReadService familyMemberReadService;
    @Autowired
    private OrderCareLinkReadService orderCareLinkReadService;
    @Autowired
    private OrderCareLinkWriteService orderCareLinkWriteService;
    @Autowired
    private OrderSuspendCareReadService orderSuspendCareReadService;





    @Override
    public void batchMigration(MoveAreaDTO moveAreaDTO, UserAccountVo userAccountVo){

        if(CommonUtil.invalidArgs(moveAreaDTO.getMoveOutAreaCode()) || moveAreaDTO.getMoveOutAreaCode().endsWith("000")) {
            throw new BizException(FailureCode.ERR_50000);
        }


        areaMoveReadService.getExistNoFinishIdcard(moveAreaDTO.getIdcardList());


        orderSuspendCareReadService.getExistSuspendOrderId(moveAreaDTO.getOrderIdList());


        List<OrderEntity> orderEntityList = orderReadService.getByIdList(moveAreaDTO.getOrderIdList());


        this.insertMoveOrder(orderEntityList, moveAreaDTO, userAccountVo);
    }


    public void insertMoveOrder(List<OrderEntity> oldOrderEntityList, MoveAreaDTO moveAreaDTO, UserAccountVo userAccountVo){

        String nowMonth = cn.hutool.core.date.DateUtil.beginOfMonth(new Date()).toString("yyyy-MM-dd");


        List<OrderEntity> newOrderEntityList = new ArrayList<>();

        List<OrderDetailEntity> orderDetailEntityList = new ArrayList<>();

        List<AreaMoveEntity> areaMoveEntityList = new ArrayList<>();

        List<AreaMoveRecordEntity> areaMoveRecordEntityList = new ArrayList<>();
        for(OrderEntity oldOrderEntity : oldOrderEntityList){

            OrderEntity newOrderEntity = this.convertNewOrder(oldOrderEntity, userAccountVo, moveAreaDTO);
            newOrderEntityList.add(newOrderEntity);


            orderDetailEntityList.add(this.convertOrderDetail(newOrderEntity, userAccountVo, moveAreaDTO));


            orderWriteService.updateOrderRecheckFlag(oldOrderEntity.getOrderId(), Constant.RecheckFlag.YES, oldOrderEntity.getVersion());


            areaMoveEntityList.add(this.convertAreaMove(oldOrderEntity, newOrderEntity, moveAreaDTO));


            areaMoveRecordEntityList.add(this.convertMoveDetail(oldOrderEntity, newOrderEntity, userAccountVo));


            familySalaryWriteService.copySalaryByOrderId(oldOrderEntity.getOrderId(), newOrderEntity.getOrderId(), nowMonth);


            familyMemberSalaryWriteService.copySalaryByOrderId(oldOrderEntity.getOrderId(), newOrderEntity.getOrderId(), nowMonth);

        }


        orderWriteService.inserts(newOrderEntityList);


        orderDetailWriteService.inserts(orderDetailEntityList);


        areaMoveWriteService.inserts(areaMoveEntityList);


        areaMoveRecordWriteService.inserts(areaMoveRecordEntityList);


        if(StringUtils.isNotBlank(moveAreaDTO.getOrderCertIds())){
            String [] certArr = moveAreaDTO.getOrderCertIds().split(",");
            List<String> orderCertIdList = Stream.of(certArr).collect(Collectors.toList());
            int rst = orderCertWriteDao.updateById(orderCertIdList, areaMoveEntityList.get(0).getOrderId(), "8", areaMoveEntityList.get(0).getId());
            if(rst <= 0){
                throw new BizException("-1", "迁移申请的证明材料操作 异常");
            }
        }
    }


    public OrderEntity convertNewOrder(OrderEntity oldOrderEntity, UserAccountVo userAccountVo, MoveAreaDTO moveAreaDTO){
        OrderEntity newOrderEntity = new OrderEntity();
        BeanUtils.copyProperties(oldOrderEntity, newOrderEntity);
        newOrderEntity.setSourOrderId(oldOrderEntity.getOrderId());
        newOrderEntity.setOrderId(Constant.getUUID());
        newOrderEntity.setCommitDate(new Date());
        newOrderEntity.setRecheckFlag(Constant.RecheckFlag.NO);
        newOrderEntity.setCreateBy(userAccountVo.getUserId());
        newOrderEntity.setCreateTime(new Date());
        newOrderEntity.setUpdateBy(userAccountVo.getUserId());
        newOrderEntity.setUpdateTime(new Date());
        newOrderEntity.setOrgId(userAccountVo.getOrgId());

        newOrderEntity.setAreaCode(moveAreaDTO.getMoveOutAreaCode());
        newOrderEntity.setStreetCode(moveAreaDTO.getMoveOutAreaCode().substring(0, 9));
        newOrderEntity.setCountyCode(moveAreaDTO.getMoveOutAreaCode().substring(0, 6));

        newOrderEntity.setBizState(Constant.BizState.APPROVE_ING);

        newOrderEntity.setType(Constant.OrderType.MOVE_OUT);

        newOrderEntity.setState(Constant.BizState.APPROVE_ING);
        newOrderEntity.setVersion("1");
        newOrderEntity.setStartMonth(null);
        return newOrderEntity;
    }


    public OrderDetailEntity convertOrderDetail(OrderEntity newOrderEntity, UserAccountVo userAccountVo, MoveAreaDTO moveAreaDTO){
        OrderDetailEntity orderDetailEntity = new OrderDetailEntity();
        orderDetailEntity.setId(Constant.getUUID());
        orderDetailEntity.setOrderId(newOrderEntity.getOrderId());

        orderDetailEntity.setCheckOpinion(moveAreaDTO.getMoveOutRemark());
        orderDetailEntity.setCheckResult(Constant.AudtiOrApprovalResult.YES);

        orderDetailEntity.setCheckPeople(moveAreaDTO.getOperator());
        orderDetailEntity.setCheckDate(DateUtil.getDate());
        orderDetailEntity.setOperator(userAccountVo.getUsername());
        orderDetailEntity.setCheckType(Constant.CheckDatailStatus.he);
        orderDetailEntity.setBusinessStatus(Constant.RemarkOperType.STREET);
        orderDetailEntity.setCreateBy(userAccountVo.getUserId());
        orderDetailEntity.setOrgId(userAccountVo.getOrgId());

        return orderDetailEntity;
    }


    public AreaMoveEntity convertAreaMove(OrderEntity oldOrderEntity, OrderEntity newOrderEntity, MoveAreaDTO moveAreaDTO){
        AreaMoveEntity areaMoveDTO = new AreaMoveEntity();
        areaMoveDTO.setId(Constant.getUUID());
        areaMoveDTO.setOrderId(newOrderEntity.getOrderId());
        areaMoveDTO.setFamilyId(newOrderEntity.getFamilyId());
        areaMoveDTO.setRemoveTime(DateUtil.formatDateTime());
        areaMoveDTO.setAreaCode(oldOrderEntity.getAreaCode());
        areaMoveDTO.setAreaCodeNew(moveAreaDTO.getMoveOutAreaCode());
        areaMoveDTO.setRemark(moveAreaDTO.getMoveOutRemark());

        return areaMoveDTO;
    }


    public AreaMoveRecordEntity convertMoveDetail(OrderEntity oldOrderEntity, OrderEntity newOrderEntity, UserAccountVo userAccountVo){
        AreaMoveRecordEntity areaMoveRecord = new AreaMoveRecordEntity();
        areaMoveRecord.setId(Constant.getUUID());
        areaMoveRecord.setFamilyId(oldOrderEntity.getFamilyId());
        areaMoveRecord.setCreateTime(DateUtil.getCurrentDate());
        areaMoveRecord.setBusinessType(oldOrderEntity.getBusinessType());
        areaMoveRecord.setOperName(userAccountVo.getUsername());
        areaMoveRecord.setOperator(userAccountVo.getUserId());
        areaMoveRecord.setOrgId(userAccountVo.getOrgId());

        areaMoveRecord.setOperType(Constant.OrderType.MOVE_IN);

        areaMoveRecord.setState(Constant.AreaMoveStatus.NEW);
        areaMoveRecord.setOrderId(newOrderEntity.getOrderId());

        return areaMoveRecord;
    }







    @Override
    public void confirmMigration(MoveAreaConfirmDTO moveAreaConfirmDTO, UserAccountVo userAccountVo){


        List<OrderFamilyDTO> moveOrderEntityList = orderReadService.getOrderFamilyByOrderIdList(new ArrayList<>(moveAreaConfirmDTO.getOrderIdMoveRecordIdMap().keySet()));


        this.confirmDealMoveOrder(moveAreaConfirmDTO, userAccountVo, moveOrderEntityList);
    }


    public void confirmDealMoveOrder(MoveAreaConfirmDTO moveAreaConfirmDTO, UserAccountVo userAccountVo,
                                     List<OrderFamilyDTO> moveOrderEntityList){
        for(OrderFamilyDTO orderFamilyDTO : moveOrderEntityList){

            if("0".equals(moveAreaConfirmDTO.getCheckResult())){

                this.rejectDealMoveOrder(moveAreaConfirmDTO, userAccountVo, moveOrderEntityList);
                return;
            }

            if("1".equals(moveAreaConfirmDTO.getProcessType())){
                //updateAreaMoveRecord(param,orderId,Constant.AreaMoveStatus.APPROVAL);
                if(!Constant.UserLevel.COUNTY.equals(userAccountVo.getAreaLevel())){
                    throw new BizException("-1","请使用组织机构为区县级的账号进行审核");
                }
                this.updateAreaMoveRecord(orderFamilyDTO, moveAreaConfirmDTO, userAccountVo, Constant.AreaMoveStatus.APPROVAL);
                this.insertOrderDetail(orderFamilyDTO, moveAreaConfirmDTO, userAccountVo, Constant.AudtiOrApprovalResult.YES);
            }else if("2".equals(moveAreaConfirmDTO.getProcessType())){

                if(!orderFamilyDTO.getAreaCode().equals(userAccountVo.getAreaCode())){
                    throw new BizException("-1","只有接收地才能确认接收");
                }


                fundPayReadService.verifyFundPayThrow(moveAreaConfirmDTO.getBusinessType(), userAccountVo.getAreaCode(),
                        orderFamilyDTO.getStandType(), DateUtil.getMonthString(), orderFamilyDTO.getSupplyType(),
                        Constant.FundType.CAPITAL_FUND_PAY);


                this.updateNewOrder(orderFamilyDTO, moveAreaConfirmDTO, userAccountVo);


                familySalaryWriteService.updateStartDate(orderFamilyDTO.getOrderId(), DateUtil.reduceMonths(moveAreaConfirmDTO.getRescueyears(),1));


                this.updateOldOrder(orderFamilyDTO, moveAreaConfirmDTO, userAccountVo);


                familySalaryWriteService.updateEndDate(orderFamilyDTO.getSourOrderId(), DateUtil.reduceMonths(moveAreaConfirmDTO.getRescueyears(),1));


                OrderEntity orderEntityMoveOut = orderReadService.getOrderById(orderFamilyDTO.getSourOrderId());

                FamilySalaryEntity oldFamilySalary = familySalaryReadService.getFamilySumSalaryByOrderId(orderFamilyDTO.getSourOrderId());
                List<MemberSalaryEntity> newMemberSalarys = familyMemberSalaryReadService.queryMemberSumSalaryByOrderId(orderFamilyDTO.getSourOrderId());

                salaryChangeWriteService.insertSalaryChange(oldFamilySalary, newMemberSalarys, moveAreaConfirmDTO.getRescueyears(),
                        "迁出", orderEntityMoveOut, Constant.SalaryChangeType.MOVE_OUT);



                familyMainHistoryWriteService.copyHistoryRecordByOrderId(orderFamilyDTO.getSourOrderId(), orderFamilyDTO.getOrderId());


                familyMemberHistoryWriteService.copyHistoryByOrderId(orderFamilyDTO.getSourOrderId(), orderFamilyDTO.getOrderId());


                OrderEntity orderEntityMoveIn = orderReadService.getOrderById(orderFamilyDTO.getOrderId());
                salaryChangeWriteService.insertSalaryChangeRecover(oldFamilySalary, newMemberSalarys, moveAreaConfirmDTO.getRescueyears(),
                        "迁入", orderEntityMoveIn, Constant.SalaryChangeType.MOVE_IN);


                this.updateAreaMoveRecord(orderFamilyDTO, moveAreaConfirmDTO, userAccountVo, Constant.AreaMoveStatus.CONFIRM);


                this.insertOrderDetail(orderFamilyDTO, moveAreaConfirmDTO, userAccountVo, Constant.AudtiOrApprovalResult.YES);


                this.copyCert(orderFamilyDTO.getSourOrderId(), orderFamilyDTO.getOrderId());


                List<FamilyMemberEntity> familyMemberEntities = familyMemberReadService.listByFamilyId(orderFamilyDTO.getFamilyId());

                for(FamilyMemberEntity familyMemberEntity:familyMemberEntities) {

                    OrderCareLinkEntity orderCareLinkEntity = orderCareLinkReadService.getCareIdByIdCard(familyMemberEntity.getIdcard());
                    if (orderCareLinkEntity != null) {

                        this.updateByCareId(orderCareLinkEntity.getCareId(),orderCareLinkEntity.getId(), familyMemberEntity.getId(), DateUtil.getMonthString());
                    }
                }
            }
        }
    }


    private void updateByCareId(String careId,String id,String memberId,String lastMonth){
        lastMonth = cn.hutool.core.date.DateUtil.parse(lastMonth, "yyyyMM").offset(DateField.MONTH,-1).toString("yyyy-MM-dd");

        OrderCareLinkEntity careLinkEntity = new OrderCareLinkEntity();
        careLinkEntity.setState("02");
        careLinkEntity.setId(id);
        careLinkEntity.setCareId(careId);
        careLinkEntity.setMemberId(memberId);
        careLinkEntity.setEndMonth(lastMonth);
        careLinkEntity.setUpdateTime(new Date());
        orderCareLinkWriteService.updateByCareId(careLinkEntity);
    }


    public void copyCert(String sourOrderId, String newOrderId){

        OrderCertEntity orderCert = new OrderCertEntity();
        orderCert.setOrderId(sourOrderId);
        orderCert.setAssociationTable("'1','2'");
        orderCert.setAssociationTableId(newOrderId);
        orderCertWriteService.insertCopy(orderCert);
    }


    public  void  insertOrderDetail(OrderFamilyDTO orderFamilyDTO, MoveAreaConfirmDTO moveAreaConfirmDTO,
                                     UserAccountVo userAccountVo, String orderDetailResult){

        OrderDetailEntity orderDetailEntity = new OrderDetailEntity();
        orderDetailEntity.setId(Constant.getUUID());
        orderDetailEntity.setOrderId(orderFamilyDTO.getOrderId());

        orderDetailEntity.setCheckOpinion(moveAreaConfirmDTO.getConfirmRemark());
        orderDetailEntity.setCheckResult(orderDetailResult);

        orderDetailEntity.setCheckPeople(moveAreaConfirmDTO.getConfirmOperator());
        orderDetailEntity.setCheckDate(DateUtil.getDate());
        orderDetailEntity.setOperator(userAccountVo.getUsername());
        orderDetailEntity.setCreateBy(userAccountVo.getUserId());
        orderDetailEntity.setOrgId(userAccountVo.getOrgId());


        orderDetailEntity.setCheckType(Constant.CheckDatailStatus.he);
        orderDetailEntity.setBusinessStatus(Constant.RemarkOperType.COUNTY);
        if(Constant.UserLevel.STREET.equals(userAccountVo.getAreaLevel())){
            orderDetailEntity.setCheckType(Constant.CheckDatailStatus.Pi);
            orderDetailEntity.setBusinessStatus(Constant.RemarkOperType.STREET);
        }


        orderDetailWriteService.insert(orderDetailEntity);
    }


    public void updateAreaMoveRecord(OrderFamilyDTO orderFamilyDTO, MoveAreaConfirmDTO moveAreaConfirmDTO,
                                      UserAccountVo userAccountVo, String areaMoveRecordState){
        AreaMoveRecordEntity entity = new AreaMoveRecordEntity();
        entity.setId(moveAreaConfirmDTO.getOrderIdMoveRecordIdMap().get(orderFamilyDTO.getOrderId()));
        entity.setCheckTime(new Date());
        entity.setOperator(userAccountVo.getUserId());
        entity.setOperName(userAccountVo.getUsername());
        entity.setOrgId(userAccountVo.getOrgId());
        entity.setState(areaMoveRecordState);
        areaMoveRecordWriteService.updateIgnoreNull(entity);
    }


    public void updateOldOrder(OrderFamilyDTO orderFamilyDTO, MoveAreaConfirmDTO moveAreaConfirmDTO, UserAccountVo userAccountVo){
        OrderEntity newOrderEntity = new OrderEntity();
        newOrderEntity.setOrderId(orderFamilyDTO.getSourOrderId());
        newOrderEntity.setEndMonth(DateUtil.reduceMonths(moveAreaConfirmDTO.getRescueyears(),1));
        newOrderEntity.setVersion(orderFamilyDTO.getSouOrderVersion());
        newOrderEntity.setUpdateTime(new Date());
        newOrderEntity.setUpdateBy(userAccountVo.getUserId());

        orderWriteService.updateIgnoreNull(newOrderEntity);
    }


    public void updateNewOrder(OrderFamilyDTO orderFamilyDTO, MoveAreaConfirmDTO moveAreaConfirmDTO, UserAccountVo userAccountVo){
        OrderEntity newOrderEntity = new OrderEntity();
        newOrderEntity.setOrderId(orderFamilyDTO.getOrderId());
        newOrderEntity.setState(Constant.BizState.APPROVE_YES);
        newOrderEntity.setBizState(Constant.BizState.APPROVE_YES);
        newOrderEntity.setStartMonth(moveAreaConfirmDTO.getRescueyears());
        newOrderEntity.setVersion(orderFamilyDTO.getVersion());
        newOrderEntity.setUpdateTime(new Date());
        newOrderEntity.setUpdateBy(userAccountVo.getUserId());

        orderWriteService.updateIgnoreNull(newOrderEntity);
    }







    @ApiOperation("审批批量迁移")
    @Override
    public BaseResult auditBatchMigration(MoveAreaParam param ){
        BaseResult result = new BaseResult();
        result.setMsg("");
        String msg = result.getMsg() ;
        String names = "";
        List <String> orderIds = param.getOrderIds();
        for (int i = 0; i <orderIds.size() ; i++) {
            String  orderId = orderIds.get(i);
            AreaMoveDTO dto = new AreaMoveDTO();
            //dto.setFamilyId(param.getFamilyIds().get(i));
            dto.setOrderId(orderId);
            AreaMoveDTO areaDto=   areaMoveReadService.orderGetAreaMove(dto);
            List<AreaMoveRecordDTO>  areaList =   areaMoveRecordReadService.queryIdcardAreaMove(dto);
            OrderEntity order =   orderReadService.getOrderById(orderId);
            if(null != areaDto  ){
                String selfAreaCode = param.getAreaCode().substring(0,6);
                if(areaDto.getAreaCode().substring(0,6).equals(selfAreaCode)){
                    if(Constant.AreaMoveStatus.NEW.equals(areaList.get(0).getState())){
                        updateAreaMoveRecord(param,orderId,Constant.AreaMoveStatus.APPROVAL);
                    }else{
                        msg += StringUtils.isNotEmpty(msg)?","+order.getName() :order.getName();
                    }
                }else{
                    names += StringUtils.isNotEmpty(names)?","+order.getName() :order.getName();
                }
            }
        }
        msg = getMessage("",msg,"迁移状态不为新申请");
        msg = getMessage(msg,names,"不是您辖区内，您无权操作");
        result.setMsg(msg);
        return result;
    }

    public String  getMessage(String msg ,String message,String text){
        if(StringUtils.isNotEmpty(message)){
            if(StringUtils.isNotEmpty(msg)){
                msg += ";" ;
            }
            msg += message + text;
        }

        return msg;

    }


    public void updateAreaMoveRecord(MoveAreaParam param ,String orderId,String status){
        AreaMoveRecordEntity entity = new AreaMoveRecordEntity();
        entity.setOrderId(orderId);
        entity.setCheckTime(new Date());
        entity.setOperator(param.getUserId());
        entity.setOperName(param.getUserName());
        entity.setOrgId(param.getOrgId());
        entity.setState(status);
        areaMoveRecordWriteService.updateIgnoreNull(entity);

    }



    @ApiOperation("驳回批量迁移")
    @Override
    public void rejectMigration(MoveAreaConfirmDTO moveAreaConfirmDTO, UserAccountVo userAccountVo){

        moveAreaConfirmDTO.setRescueyears(DateUtil.sdfYM.format(new Date()));


        List<OrderFamilyDTO> moveOrderEntityList = orderReadService.getOrderFamilyByOrderIdList(new ArrayList<>(moveAreaConfirmDTO.getOrderIdMoveRecordIdMap().keySet()));


        this.rejectDealMoveOrder(moveAreaConfirmDTO, userAccountVo, moveOrderEntityList);

    }


    public void rejectDealMoveOrder(MoveAreaConfirmDTO moveAreaConfirmDTO, UserAccountVo userAccountVo,
                                      List<OrderFamilyDTO> moveOrderEntityList){
        Map<String, List<String>> rstIdcardMsg = new HashMap<>();
        for(OrderFamilyDTO orderFamilyDTO : moveOrderEntityList){

//            fundPayReadService.verifyFundPayThrow(moveAreaConfirmDTO.getBusinessType(), userAccountVo.getAreaCode(),
//                    orderFamilyDTO.getStandType(), DateUtil.getMonthString(), orderFamilyDTO.getSupplyType(),
//                    Constant.FundType.CAPITAL_FUND_PAY);


            familySalaryWriteService.deleteSalaryByOrderId(orderFamilyDTO.getOrderId());


            this.updateNewOrder(orderFamilyDTO, userAccountVo);


            orderWriteService.updateOrderRecheckFlag(orderFamilyDTO.getSourOrderId(), Constant.RecheckFlag.NO.intValue(), orderFamilyDTO.getSouOrderVersion());


            String moveRecordState = Constant.AreaMoveStatus.TURN_DOWN;
            if("2".equals(moveAreaConfirmDTO.getProcessType())){
                moveRecordState = Constant.AreaMoveStatus.DISAGREE;
            }
            this.updateAreaMoveRecord(orderFamilyDTO, moveAreaConfirmDTO, userAccountVo, moveRecordState);


            this.insertOrderDetail(orderFamilyDTO, moveAreaConfirmDTO, userAccountVo, Constant.AudtiOrApprovalResult.REFUSE);
        }
    }


    public void updateNewOrder(OrderFamilyDTO orderFamilyDTO, UserAccountVo userAccountVo){
        OrderEntity orderEntity = new OrderEntity();
        orderEntity.setState(Constant.BizStateNew.APPROVE_REFUSE);
        orderEntity.setBizState(Constant.BizStateNew.APPROVE_REFUSE);
        orderEntity.setVersion(orderFamilyDTO.getVersion());
        orderEntity.setStartMonth(DateUtil.sdfYM.format(new Date()));
        orderEntity.setEndMonth(DateUtil.sdfYM.format(new Date()));
        orderEntity.setOrderId(orderFamilyDTO.getOrderId());
        orderEntity.setUpdateTime(new Date());
        orderEntity.setUpdateBy(userAccountVo.getUserId());
        orderWriteService.updateIgnoreNull(orderEntity);
    }


    @Override
    public ExcelInfo exportExcelMigMember(AreaMoveDTO param, UserAccountVo accountVo){

        QueryParamUtil.setBaseParamValue(param, accountVo);

        ExcelInfo excelInfo = null;

        List<AreaMoveRecordDTO> list = areaMoveRecordReadService.dwonloadMoveDetail(param);
        excelInfo = exportExcelService.exportOrgOpLogs(AreaMoveRecordDTO.class,list,"批量迁移对象信息.xls",
                "批量迁移对象列表","批量迁移对象列表",false,null);

        return excelInfo;
    }

    @Override
    public ExcelInfo exportMigrationList(MoveAreaParam param, UserAccountVo accountVo){

        QueryParamUtil.setBaseParamValue(param, accountVo);

        ExcelInfo excelInfo = null;

        List<SalvationFamilyExcelDTO> list = orderReadService.exportMigrationList(param,accountVo);
        excelInfo = exportExcelService.exportOrgOpLogs(SalvationFamilyExcelDTO.class,list,"批量迁移办理信息.xls",
                "批量迁移办理列表","批量迁移办理列表",false,null);

        return excelInfo;
    }
}
